package com.example.sunmiv1.activity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.example.sunmiv1.R;
import com.example.sunmiv1.adapter.StringCodeDetailAdapter;
import com.example.sunmiv1.base.BaseActivity;
import com.example.sunmiv1.base.bean;
import com.example.sunmiv1.dialog.BaseDialog;
import com.example.sunmiv1.dialog.OrderDetailEditDialog;
import com.example.sunmiv1.entity.AllocationGoodsInfo;
import com.example.sunmiv1.entity.DepositGoodsEditInfo;
import com.example.sunmiv1.entity.DepositGoodsInfo;
import com.example.sunmiv1.entity.GiftGoodsInfo;
import com.example.sunmiv1.entity.GoodBean;
import com.example.sunmiv1.entity.GoodsStoreRetreatInfo;
import com.example.sunmiv1.entity.LossGoodsInfo;
import com.example.sunmiv1.entity.OnlineOrderBean;
import com.example.sunmiv1.entity.OrderDetailInfo;
import com.example.sunmiv1.entity.ReportLossGoodBean;
import com.example.sunmiv1.entity.RetreatGoodSerialList;
import com.example.sunmiv1.entity.SerialDetailInfo;
import com.example.sunmiv1.entity.StringCodeDetailBean;
import com.example.sunmiv1.entity.WarehousingDetailInfo;
import com.example.sunmiv1.utils.Tools;
import com.example.sunmiv1.utils.net.ACache;
import com.example.sunmiv1.utils.net.Common;
import com.example.sunmiv1.utils.net.GsonUtils;
import com.example.sunmiv1.utils.net.HttpUtils;
import com.example.sunmiv1.utils.net.LoadingDialog;
import com.example.sunmiv1.utils.net.TextCallBack;
import com.example.sunmiv1.utils.zxing.Constant;
import com.example.sunmiv1.utils.zxing.activity.CaptureActivity;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class StringCodeOperationActivity extends BaseActivity {

    @BindView(R.id.title_back)
    ImageView titleBack;
    @BindView(R.id.title_title)
    TextView titleTitle;
    @BindView(R.id.stringCodeDetail_name)
    TextView stringCodeDetailName;
    @BindView(R.id.stringCodeDetail_sku)
    TextView stringCodeDetailSku;
    @BindView(R.id.stringCodeDetail_search)
    ImageView stringCodeDetailSearch;
    @BindView(R.id.stringCodeDetail_edit)
    EditText stringCodeDetailEdit;
    @BindView(R.id.stringCodeDetail_scan)
    ImageView stringCodeDetailScan;
    @BindView(R.id.stringCodeDetail_list)
    ListView stringCodeDetailList;
    @BindView(R.id.stringCodeDetail_nameLinear)
    LinearLayout stringCodeDetailNameLinear;
    @BindView(R.id.stringCodeDetail_skuLinear)
    LinearLayout stringCodeDetailSkuLinear;
    @BindView(R.id.stringCodeDetail_searchLinear)
    LinearLayout stringCodeDetailSearchLinear;
    @BindView(R.id.stringCodeDetail_mainEdit)
    EditText stringCodeDetailMainEdit;
    @BindView(R.id.stringCodeDetail_mainScan)
    ImageView stringCodeDetailMainScan;
    @BindView(R.id.stringCodeDetail_mainEdit1)
    EditText stringCodeDetailMainEdit1;
    @BindView(R.id.stringCodeDetail_mainScan1)
    ImageView stringCodeDetailMainScan1;
    @BindView(R.id.stringCodeDetail_mainEdit2)
    EditText stringCodeDetailMainEdit2;
    @BindView(R.id.stringCodeDetail_mainScan2)
    ImageView stringCodeDetailMainScan2;
    @BindView(R.id.stringCodeDetail_mainAdd)
    TextView stringCodeDetailMainAdd;
    @BindView(R.id.stringCodeDetail_addLinear)
    LinearLayout stringCodeDetailAddLinear;
    @BindView(R.id.stringCodeDetail_add)
    TextView stringCodeDetailAdd;
    @BindView(R.id.stringCodeDetail_chooseNum)
    TextView stringCodeDetailChooseNum;
    @BindView(R.id.stringCodeDetail_submit)
    TextView stringCodeDetailSubmit;
    @BindView(R.id.stringCodeDetail_submitLinear)
    LinearLayout stringCodeDetailSubmitLinear;
    @BindView(R.id.stringCodeDetail_refreshList)
    PullToRefreshListView stringCodeDetailRefreshList;

    private StringCodeDetailAdapter adapter;

    private ACache aCache;
    private Context context;
    private Intent intent;
    private LoadingDialog loadingDialog;

    /**
     * 1 CollectingSilverActivity  2 InventorySearchActivity 3 NewInventoryActivity
     * 4 InventoryInfoAdapter 5 NewReportLossActivity 6 ReportLossSearchActivity
     */
    private int operation = 1;

    private StringCodeDetailBean stringCodeDetailBean;
    private ArrayList<String> judgeList = new ArrayList<>();//用來判斷是否有重複串碼

    /**
     * operation==1
     */
    private GoodBean.DataBean.GoodsBean goodsBean;
    private List<GoodBean.DataBean.GoodsBean.SerialBean> list = new ArrayList<>();

    /**
     * operation==2,3  盤點
     */
    private ReportLossGoodBean.DataBean.ListBean reportLossBean;
    private List<ReportLossGoodBean.DataBean.ListBean.SerialBean> list1 = new ArrayList<>();

    /**
     * operation==4盤點詳情 10採購單詳情 13採購退貨詳情 (商戶 17出庫單詳情 18入庫單詳情 19拒收詳情 20調撥詳情 21調撥單入庫詳情)
     * 22門店入庫單調撥詳情(商戶調撥) 23門店入庫單調撥詳情(門店調撥) 24門店入庫單入庫詳情 25報損詳情 26銷售明細列表詳情 29，30盤點系統庫存詳情
     */
    private String id = "";
    private int page = 1;
    private SerialDetailInfo serialDetailInfo;
    private List<SerialDetailInfo.DataBean.ListBean> list2 = new ArrayList<>();

    /**
     * operation==5,6  報損
     */
    private LossGoodsInfo.DataBean lossGoodsInfo;
    private List<LossGoodsInfo.DataBean.SerialBean> list3 = new ArrayList<>();

    /**
     * operation==7,8,9  採購單
     */
    private DepositGoodsInfo.DataBean.ListBean depositGoodsInfo;
    private List<DepositGoodsInfo.DataBean.ListBean.SerialBean> list4 = new ArrayList<>();

    /**
     * operation==11 編輯採購單
     */
    private DepositGoodsEditInfo.DataBean depositGoodsEditInfo;
    private List<DepositGoodsEditInfo.DataBean.SerialListBean> list5 = new ArrayList<>();

    /**
     * operation==12 採購退貨
     */
    private RetreatGoodSerialList retreatGoodSerialList;
    private GoodsStoreRetreatInfo.DataBean storeRetreatInfo;
    private List<GoodsStoreRetreatInfo.DataBean.SerialListBean> list6 = new ArrayList<>();

    /**
     * operation==14 operation==15 出庫單
     */
    private AllocationGoodsInfo.DataBean allocationGoodsInfo;
    private List<AllocationGoodsInfo.DataBean.SerialListBean> list7 = new ArrayList<>();

    /**
     * operation==16 入庫單
     */
    private WarehousingDetailInfo.DataBean warehousingDetailInfo;
    private List<WarehousingDetailInfo.DataBean.SerialListBean> list8 = new ArrayList<>();

    /**
     * operation==27 配送選擇商品串碼
     */
    private OrderDetailInfo.DataBean orderDetailInfo;
    private List<OrderDetailInfo.DataBean.SerialListBean> list9 = new ArrayList<>();


    /**
     * operation==27 配送選擇商品串碼
     * 28 查看線上訂單詳情
     */
    private OnlineOrderBean.OrderBean.GoodInfoBean onlineOrderBean;
    private List<OnlineOrderBean.OrderBean.GoodInfoBean.SerialListBean> list10 = new ArrayList<>();

    private int scanType = 0;


    @Override
    protected void initView() {
        setContentView(R.layout.activity_string_code_operation);
        ButterKnife.bind(this);
        titleTitle.setText("選擇串碼");
        context = this;
        aCache = ACache.get(context);
        loadingDialog = new LoadingDialog(context);
        setAdapter();
    }

    @Override
    protected void initData() {
        operation = getIntent().getIntExtra("operation", 1);
        setFilter();
        change();
    }

    private void setFilter() {
        InputFilter typeFilter = new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
//                Log.e("TAG", "filter: source==========="+source);
                Pattern p = Pattern.compile("[0-9a-zA-Z]+");
                Matcher m = p.matcher(source.toString());
                if (!m.matches() && !TextUtils.isEmpty(source.toString())) {
                    Toast.makeText(context, "掃描的條碼有問題，請重新掃描", Toast.LENGTH_SHORT).show();
                    return "";
                }
                return null;
            }
        };
        stringCodeDetailEdit.setFilters(new InputFilter[]{typeFilter, new InputFilter.LengthFilter(20)});
        stringCodeDetailMainEdit.setFilters(new InputFilter[]{typeFilter, new InputFilter.LengthFilter(20)});
        stringCodeDetailMainEdit1.setFilters(new InputFilter[]{typeFilter, new InputFilter.LengthFilter(20)});
        stringCodeDetailMainEdit2.setFilters(new InputFilter[]{typeFilter, new InputFilter.LengthFilter(20)});
    }

    private void setAdapter() {
        if (adapter == null) {
            adapter = new StringCodeDetailAdapter(new ArrayList<>());
//            if (operation == 4) {
//                stringCodeDetailRefreshList.setAdapter(adapter);
//            } else {
            stringCodeDetailList.setAdapter(adapter);
            stringCodeDetailRefreshList.setAdapter(adapter);
//            }
        } else {
            adapter.notifyDataSetChanged();
        }
        adapter.CallBack((type, position) -> {
            //1刪除 2編輯 3保存 4修改 5記錄
            switch (type) {
                case 1:
                    switch (operation) {
                        case 1:
                            if (judgeList.indexOf(list.get(position).getMain_serial_number()) != -1) {
                                judgeList.remove(judgeList.indexOf(list.get(position).getMain_serial_number()));
                            }
                            if (judgeList.indexOf(list.get(position).getAuxiliary_serial_number_one()) != -1) {
                                judgeList.remove(judgeList.indexOf(list.get(position).getAuxiliary_serial_number_one()));
                            }
                            if (judgeList.indexOf(list.get(position).getAuxiliary_serial_number_two()) != -1) {
                                judgeList.remove(judgeList.indexOf(list.get(position).getAuxiliary_serial_number_two()));
                            }
                            list.remove(position);
                            break;
                        case 2:
                        case 3:
                            if (judgeList.indexOf(list1.get(position).getMain_serial_number()) != -1) {
                                judgeList.remove(judgeList.indexOf(list1.get(position).getMain_serial_number()));
                            }
                            if (judgeList.indexOf(list1.get(position).getAuxiliary_serial_number_one()) != -1) {
                                judgeList.remove(judgeList.indexOf(list1.get(position).getAuxiliary_serial_number_one()));
                            }
                            if (judgeList.indexOf(list1.get(position).getAuxiliary_serial_number_two()) != -1) {
                                judgeList.remove(judgeList.indexOf(list1.get(position).getAuxiliary_serial_number_two()));
                            }
                            list1.remove(position);
                            break;
                        case 5:
                        case 6:
                            if (judgeList.indexOf(list3.get(position).getMain_serial_number()) != -1) {
                                judgeList.remove(judgeList.indexOf(list3.get(position).getMain_serial_number()));
                            }
                            if (judgeList.indexOf(list3.get(position).getAuxiliary_serial_number_one()) != -1) {
                                judgeList.remove(judgeList.indexOf(list3.get(position).getAuxiliary_serial_number_one()));
                            }
                            if (judgeList.indexOf(list3.get(position).getAuxiliary_serial_number_two()) != -1) {
                                judgeList.remove(judgeList.indexOf(list3.get(position).getAuxiliary_serial_number_two()));
                            }
                            list3.remove(position);
                            break;
                        case 7:
                        case 8:
                        case 9:
                            if (judgeList.indexOf(list4.get(position).getMain_serial_number()) != -1) {
                                judgeList.remove(judgeList.indexOf(list4.get(position).getMain_serial_number()));
                            }
                            if (judgeList.indexOf(list4.get(position).getAuxiliary_serial_number_one()) != -1) {
                                judgeList.remove(judgeList.indexOf(list4.get(position).getAuxiliary_serial_number_one()));
                            }
                            if (judgeList.indexOf(list4.get(position).getAuxiliary_serial_number_two()) != -1) {
                                judgeList.remove(judgeList.indexOf(list4.get(position).getAuxiliary_serial_number_two()));
                            }
                            list4.remove(position);
                            break;
                        case 11:
                            if (judgeList.indexOf(list5.get(position).getMain_serial_number()) != -1) {
                                judgeList.remove(judgeList.indexOf(list5.get(position).getMain_serial_number()));
                            }
                            if (judgeList.indexOf(list5.get(position).getAuxiliary_serial_number_one()) != -1) {
                                judgeList.remove(judgeList.indexOf(list5.get(position).getAuxiliary_serial_number_one()));
                            }
                            if (judgeList.indexOf(list5.get(position).getAuxiliary_serial_number_two()) != -1) {
                                judgeList.remove(judgeList.indexOf(list5.get(position).getAuxiliary_serial_number_two()));
                            }
                            list5.remove(position);
                            break;
                        case 12:
                            if (judgeList.indexOf(list6.get(position).getMain_serial_number()) != -1) {
                                judgeList.remove(judgeList.indexOf(list6.get(position).getMain_serial_number()));
                            }
                            if (judgeList.indexOf(list6.get(position).getAuxiliary_serial_number_one()) != -1) {
                                judgeList.remove(judgeList.indexOf(list6.get(position).getAuxiliary_serial_number_one()));
                            }
                            if (judgeList.indexOf(list6.get(position).getAuxiliary_serial_number_two()) != -1) {
                                judgeList.remove(judgeList.indexOf(list6.get(position).getAuxiliary_serial_number_two()));
                            }
                            list6.remove(position);
                            break;
                        case 14:
                        case 15:
                            if (judgeList.indexOf(list7.get(position).getMain_serial_number()) != -1) {
                                judgeList.remove(judgeList.indexOf(list7.get(position).getMain_serial_number()));
                            }
                            if (judgeList.indexOf(list7.get(position).getAuxiliary_serial_number_one()) != -1) {
                                judgeList.remove(judgeList.indexOf(list7.get(position).getAuxiliary_serial_number_one()));
                            }
                            if (judgeList.indexOf(list7.get(position).getAuxiliary_serial_number_two()) != -1) {
                                judgeList.remove(judgeList.indexOf(list7.get(position).getAuxiliary_serial_number_two()));
                            }
                            list7.remove(position);
                            break;
                        case 27:
                            if (judgeList.indexOf(list9.get(position).getMain_serial_number()) != -1) {
                                judgeList.remove(judgeList.indexOf(list9.get(position).getMain_serial_number()));
                            }
                            if (judgeList.indexOf(list9.get(position).getAuxiliary_serial_number_one()) != -1) {
                                judgeList.remove(judgeList.indexOf(list9.get(position).getAuxiliary_serial_number_one()));
                            }
                            if (judgeList.indexOf(list9.get(position).getAuxiliary_serial_number_two()) != -1) {
                                judgeList.remove(judgeList.indexOf(list9.get(position).getAuxiliary_serial_number_two()));
                            }
                            list9.remove(position);
                            break;

                    }
                    adapter.notifyDataSetChanged();
                    break;
                case 2:
                    break;
                case 3:
                    switch (operation) {
                        case 16:
                            list8.get(position).setCheck(!list8.get(position).isCheck());
                            setSize();
                            adapter.setList(list8);
                            break;
                    }
                    break;
                case 4:
                    OrderDetailEditDialog.newInstance(onlineOrderBean, list10.get(position), judgeList)
                            .callback(o -> {
                                list10.set(position, (OnlineOrderBean.OrderBean.GoodInfoBean.SerialListBean) o);
                                adapter.setList(list10);
                                onlineOrderBean.setSerial_list(list10);
                            })
                            .startShow(getSupportFragmentManager(), "")
                            .setCancelable(false);
                    break;
                case 5:
                    intent = new Intent(context, ModifyRecordActivity.class);
                    intent.putExtra("id", list10.get(position).getId());
                    startActivity(intent);
                    break;
            }
            setSize();
        });
    }

    @Override
    protected void initListener() {
        if (operation == 4 || operation == 10 || operation == 13 || operation == 17 || operation == 18 || operation == 19
                || operation == 20 || operation == 21 || operation == 22 || operation == 23 || operation == 24
                || operation == 25 || operation == 29 || operation == 30) {
            stringCodeDetailRefreshList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
                @Override
                public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                    page = 1;
                    list2.clear();
                    adapter.setList(list2);
                    getSerialDetail();

                }

                @Override
                public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                    getSerialDetail();
                }
            });
        }
        stringCodeDetailEdit.setOnEditorActionListener((v, actionId, event) -> {
            if (getBoolean(actionId, event)) {
                if(operation==1){
                    if (TextUtils.isEmpty(stringCodeDetailEdit.getText().toString())) {
                        Toast.makeText(context, "請輸入要搜索的串號", Toast.LENGTH_SHORT).show();
                        return true;
                    }
                    if (judgeList.indexOf(stringCodeDetailEdit.getText().toString().trim()) != -1) {
                        Toast.makeText(context, "串碼重複", Toast.LENGTH_SHORT).show();
                        return true;
                    }
                    if (operation == 2 || operation == 3|| operation == 1) {
                        searchStringCode();
                    } else {
                        chooseGoodSerial();
                    }
                }else if (operation == 4 || operation == 10 || operation == 13 || operation == 17 || operation == 18 || operation == 19
                        || operation == 20 || operation == 21 || operation == 22 || operation == 23 || operation == 24
                        || operation == 25 || operation == 29 || operation == 30) {
                    list2.clear();
                    adapter.setList(list2);
                    page = 1;
                    getSerialDetail();
                } else if (operation == 12) {
                    if (TextUtils.isEmpty(stringCodeDetailEdit.getText().toString().trim())) {
                        Toast.makeText(context, "請輸入要搜索的串碼", Toast.LENGTH_SHORT).show();
                        return true;
                    }
                    if (judgeList.indexOf(stringCodeDetailEdit.getText().toString().trim()) != -1) {
                        Toast.makeText(context, "串碼重複", Toast.LENGTH_SHORT).show();
                        return true;
                    }
                    retreatGoodSerial();
                } else if (operation == 14 || operation == 15) {
                    if (TextUtils.isEmpty(stringCodeDetailEdit.getText().toString().trim())) {
                        Toast.makeText(context, "請輸入要搜索的串碼", Toast.LENGTH_SHORT).show();
                        return true;
                    }
                    if (judgeList.indexOf(stringCodeDetailEdit.getText().toString().trim()) != -1) {
                        Toast.makeText(context, "串碼重複", Toast.LENGTH_SHORT).show();
                        return true;
                    }
                    retreatGoodSerial();
                } else if (operation == 27) {
                    if (TextUtils.isEmpty(stringCodeDetailEdit.getText().toString().trim())) {
                        Toast.makeText(context, "請輸入要搜索的串碼", Toast.LENGTH_SHORT).show();
                        return true;
                    }

                    if (judgeList.indexOf(stringCodeDetailEdit.getText().toString().trim()) != -1) {
                        Toast.makeText(context, "串碼重複", Toast.LENGTH_SHORT).show();
                        return true;
                    }
                    if (list9.size() >= orderDetailInfo.getNum()) {
                        Toast.makeText(context, "不能繼續添加了", Toast.LENGTH_SHORT).show();
                        return true;
                    }
                    getDeliverySerial();
                }
            }
            return true;
        });
    }

    /**
     * 判斷是否點擊回車事件
     */
    private boolean getBoolean(int actionId, KeyEvent event) {
        boolean e = actionId == EditorInfo.IME_ACTION_SEND
                || actionId == EditorInfo.IME_ACTION_DONE
                || (event != null && KeyEvent.KEYCODE_ENTER == event.getKeyCode() && KeyEvent.ACTION_DOWN == event.getAction());
        return e;
    }

    @OnClick({R.id.title_back, R.id.stringCodeDetail_search, R.id.stringCodeDetail_scan, R.id.stringCodeDetail_add,
            R.id.stringCodeDetail_mainScan, R.id.stringCodeDetail_mainScan1, R.id.stringCodeDetail_mainScan2, R.id.stringCodeDetail_mainAdd,
            R.id.stringCodeDetail_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                if (operation == 28) {
                    onlineOrderBean.setSerial_list(list10);
                    intent = new Intent(context, OnlineOrderDetailActivity.class);
                    intent.putExtra("bean", onlineOrderBean);
                    setResult(RESULT_OK, intent);
                    finish();
                } else {
                    finish();
                }
                break;
            case R.id.stringCodeDetail_search:
                if (operation == 4 || operation == 10 || operation == 13 || operation == 17 || operation == 18 || operation == 19
                        || operation == 20 || operation == 21 || operation == 22 || operation == 23 || operation == 24
                        || operation == 25 || operation == 29 || operation == 30) {
                    list2.clear();
                    adapter.setList(list2);
                    page = 1;
                    getSerialDetail();
                } else if (operation == 12) {
                    if (TextUtils.isEmpty(stringCodeDetailEdit.getText().toString().trim())) {
                        Toast.makeText(context, "請輸入要搜索的串碼", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (judgeList.indexOf(stringCodeDetailEdit.getText().toString().trim()) != -1) {
                        Toast.makeText(context, "串碼重複", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    retreatGoodSerial();
                } else if (operation == 14 || operation == 15) {
                    if (TextUtils.isEmpty(stringCodeDetailEdit.getText().toString().trim())) {
                        Toast.makeText(context, "請輸入要搜索的串碼", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (judgeList.indexOf(stringCodeDetailEdit.getText().toString().trim()) != -1) {
                        Toast.makeText(context, "串碼重複", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    retreatGoodSerial();
                } else if (operation == 27) {
                    if (TextUtils.isEmpty(stringCodeDetailEdit.getText().toString().trim())) {
                        Toast.makeText(context, "請輸入要搜索的串碼", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (judgeList.indexOf(stringCodeDetailEdit.getText().toString().trim()) != -1) {
                        Toast.makeText(context, "串碼重複", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (list9.size() >= orderDetailInfo.getNum()) {
                        Toast.makeText(context, "不能繼續添加了", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    getDeliverySerial();
                }
                break;
            case R.id.stringCodeDetail_add:
                if (TextUtils.isEmpty(stringCodeDetailEdit.getText().toString())) {
                    Toast.makeText(context, "請輸入要搜索的串號", Toast.LENGTH_SHORT).show();
                    return;
                }
               if (judgeList.indexOf(stringCodeDetailEdit.getText().toString().trim()) != -1) {
                    Toast.makeText(context, "串碼重複", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (operation == 2 || operation == 3|| operation == 1) {
                    searchStringCode();
                } else {
                    chooseGoodSerial();
                }

                break;
            case R.id.stringCodeDetail_scan:
                scanType = 0;
                startQrCode();
                break;
            case R.id.stringCodeDetail_mainScan:
                scanType = 1;
                startQrCode();
                break;
            case R.id.stringCodeDetail_mainScan1:
                scanType = 2;
                startQrCode();
                break;
            case R.id.stringCodeDetail_mainScan2:
                scanType = 3;
                startQrCode();
                break;
            case R.id.stringCodeDetail_mainAdd:
                if (TextUtils.isEmpty(stringCodeDetailMainEdit.getText().toString().trim())) {
                    Toast.makeText(context, "請輸入主串碼", Toast.LENGTH_SHORT).show();
                    return;
                }
                switch (operation) {
                    case 2:
                    case 3:
                        if (!judgeRepeat()) {//判斷是否有重複的串碼
                            judgeList.add(stringCodeDetailMainEdit.getText().toString().trim());
                            if (!TextUtils.isEmpty(stringCodeDetailMainEdit1.getText().toString().trim())) {
                                judgeList.add(stringCodeDetailMainEdit1.getText().toString().trim());
                            }
                            if (!TextUtils.isEmpty(stringCodeDetailMainEdit2.getText().toString().trim())) {
                                judgeList.add(stringCodeDetailMainEdit2.getText().toString().trim());
                            }
                            list1.add(new ReportLossGoodBean.DataBean.ListBean.SerialBean(stringCodeDetailMainEdit.getText().toString(),
                                    stringCodeDetailMainEdit1.getText().toString().trim(), stringCodeDetailMainEdit2.getText().toString().trim()));
                            stringCodeDetailMainEdit.setText("");
                            stringCodeDetailMainEdit1.setText("");
                            stringCodeDetailMainEdit2.setText("");
                            adapter.setList(list1);
                            Toast.makeText(context, "錄入成功", Toast.LENGTH_SHORT).show();
                        }
                        break;
                    case 5:
                    case 6:
                        if (!judgeRepeat()) {//判斷是否有重複的串碼
                            judgeList.add(stringCodeDetailMainEdit.getText().toString().trim());
                            if (!TextUtils.isEmpty(stringCodeDetailMainEdit1.getText().toString().trim())) {
                                judgeList.add(stringCodeDetailMainEdit1.getText().toString().trim());
                            }
                            if (!TextUtils.isEmpty(stringCodeDetailMainEdit2.getText().toString().trim())) {
                                judgeList.add(stringCodeDetailMainEdit2.getText().toString().trim());
                            }
                            chooseGoodSerial();
                        }
                        break;
                    case 7:
                    case 8:
                    case 9:
                        if (!judgeRepeat()) {//判斷是否有重複的串碼
                            judgeList.add(stringCodeDetailMainEdit.getText().toString().trim());
                            if (!TextUtils.isEmpty(stringCodeDetailMainEdit1.getText().toString().trim())) {
                                judgeList.add(stringCodeDetailMainEdit1.getText().toString().trim());
                            }
                            if (!TextUtils.isEmpty(stringCodeDetailMainEdit2.getText().toString().trim())) {
                                judgeList.add(stringCodeDetailMainEdit2.getText().toString().trim());
                            }
                            list4.add(new DepositGoodsInfo.DataBean.ListBean.SerialBean(stringCodeDetailMainEdit.getText().toString(),
                                    stringCodeDetailMainEdit1.getText().toString().trim(), stringCodeDetailMainEdit2.getText().toString().trim()));
                            stringCodeDetailMainEdit.setText("");
                            stringCodeDetailMainEdit1.setText("");
                            stringCodeDetailMainEdit2.setText("");
                            adapter.setList(list4);
                            Toast.makeText(context, "錄入成功", Toast.LENGTH_SHORT).show();
                        }
                        break;
                    case 11:
                        if (!judgeRepeat()) {//判斷是否有重複的串碼
                            judgeList.add(stringCodeDetailMainEdit.getText().toString().trim());
                            if (!TextUtils.isEmpty(stringCodeDetailMainEdit1.getText().toString().trim())) {
                                judgeList.add(stringCodeDetailMainEdit1.getText().toString().trim());
                            }
                            if (!TextUtils.isEmpty(stringCodeDetailMainEdit2.getText().toString().trim())) {
                                judgeList.add(stringCodeDetailMainEdit2.getText().toString().trim());
                            }
                            list5.add(new DepositGoodsEditInfo.DataBean.SerialListBean(stringCodeDetailMainEdit.getText().toString(),
                                    stringCodeDetailMainEdit1.getText().toString().trim(), stringCodeDetailMainEdit2.getText().toString().trim()));
                            stringCodeDetailMainEdit.setText("");
                            stringCodeDetailMainEdit1.setText("");
                            stringCodeDetailMainEdit2.setText("");
                            adapter.setList(list5);
                            Toast.makeText(context, "錄入成功", Toast.LENGTH_SHORT).show();
                        }
                        break;
                }
                setSize();
                break;
            case R.id.stringCodeDetail_submit:
                switch (operation) {
                    case 1:
                        Log.d("串碼數量", String.valueOf(goodsBean.getSerialList().size()));
                        if (list.size()>0||goodsBean.getSerialList().size()>0) {
                            goodsBean.setSerialList((List<GoodBean.DataBean.GoodsBean.SerialBean>) list);
                            goodsBean.setAgoods_count((double) list.size());
                            intent = new Intent(context, CollectingSilverActivity.class);
                            intent.putExtra("bean", goodsBean);
                            setResult(100, intent);
                            finish();
                        }else {
                            Toast.makeText(context, "請先選擇商品串碼", Toast.LENGTH_SHORT).show();
                        }
                        break;
                    case 2:
                    case 3:
                        inventorySerialVerify();
                        break;
                    case 5:
                    case 6:
                        lossGoodsInfo.setSerialList(list3);
                        lossGoodsInfo.setLoss_num(list3.size() + "");
                        if (operation == 5) {
                            intent = new Intent(context, NewReportLossActivity.class);
                        } else {
                            intent = new Intent(context, ReportLossSearchActivity.class);
                        }
                        intent.putExtra("bean", lossGoodsInfo);
                        setResult(RESULT_OK, intent);
                        finish();
                        break;
                    case 7:
                    case 8:
                    case 9:
                    case 11:
                        depositSerialVerify();
                        break;
                    case 12:
                        storeRetreatInfo.setSerial_list(list6);
                        storeRetreatInfo.setRetreat_num_act((double) list6.size());
                        intent = new Intent(context, PurchaseRefundAddingActivity.class);
                        intent.putExtra("bean", storeRetreatInfo);
                        setResult(RESULT_OK, intent);
                        finish();
                        break;
                    case 14:
                    case 15:
                        allocationGoodsInfo.setSerial_list(list7);
                        allocationGoodsInfo.setOutput_num(list7.size() + "");
                        if (operation == 14) {
                            intent = new Intent(context, AddAllocationActivity.class);
                        } else {
                            intent = new Intent(context, AllocationGoodsActivity.class);
                        }
                        intent.putExtra("bean", allocationGoodsInfo);
                        setResult(RESULT_OK, intent);
                        finish();
                        break;
                    case 16:
                        warehousingDetailInfo.setSerialList(list8);
                        int s = 0;
                        for (WarehousingDetailInfo.DataBean.SerialListBean bean : list8) {
                            if (!bean.isCheck()) {
                                s++;
                            }
                        }
                        warehousingDetailInfo.setWarehouse_num(s);
                        intent = new Intent(context, AddAllocationActivity.class);
                        intent.putExtra("bean", warehousingDetailInfo);
                        setResult(RESULT_OK, intent);
                        finish();
                        break;
                    case 27:
                        if (list9.size() > orderDetailInfo.getNum()) {
                            Toast.makeText(context, "添加數量不能大於配送數量", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        if (list9.size() < orderDetailInfo.getNum()) {
                            Toast.makeText(context, "添加數量不能小於配送數量", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        orderDetailInfo.setSerial_list(list9);
                        intent = new Intent(context, DeliveryGoodsActivity.class);
                        intent.putExtra("bean", orderDetailInfo);
                        setResult(RESULT_OK, intent);
                        finish();
                        break;
                    case 28:
                        onlineOrderBean.setSerial_list(list10);
                        intent = new Intent(context, OnlineOrderDetailActivity.class);
                        intent.putExtra("bean", onlineOrderBean);
                        setResult(RESULT_OK, intent);
                        finish();
                        break;

                }
                break;
        }
    }

    private void searchStringCode() {
        loadingDialog.show();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("goods_id", goodsBean.getId());
            if ("2".equals(goodsBean.getAttribute_state()) && goodsBean.getSkujson().size() > 0) {
                jsonObject.put("sku_id", goodsBean.getSkujson().get(0).getId());
            }
            jsonObject.put("serial_number", stringCodeDetailEdit.getText().toString().trim());
            HttpUtils.post(context, Common.SEARCH_SERIAL, Tools.createParameterCode(jsonObject), new TextCallBack() {
                @Override
                protected void onSuccess(String text) {
                    loadingDialog.dismiss();
                    try {
                        Log.d("測試15",text);
                        JSONObject json = new JSONObject(text);
                        if (json.getInt("status") == 1) {
                            stringCodeDetailBean = GsonUtils.JsonToBean(text, StringCodeDetailBean.class);
                            StringCodeDetailBean.DataBean bean = stringCodeDetailBean.getData();
                            for (GoodBean.DataBean.GoodsBean.SerialBean bean1 : list) {
                                if (!TextUtils.isEmpty(bean.getMain_serial_number())&&bean1.getMain_serial_number().equals(bean.getMain_serial_number())) {
                                    Toast.makeText(context, "串碼已存在", Toast.LENGTH_SHORT).show();
                                    return;
                                }
                                if (!TextUtils.isEmpty(bean.getAuxiliary_serial_number_one())&&bean1.getAuxiliary_serial_number_one().equals(bean.getAuxiliary_serial_number_one())) {
                                    Toast.makeText(context, "串碼已存在", Toast.LENGTH_SHORT).show();
                                    return;
                                }
                                if (!TextUtils.isEmpty(bean.getAuxiliary_serial_number_two())&&bean1.getAuxiliary_serial_number_two().equals(bean.getAuxiliary_serial_number_two())) {
                                    Toast.makeText(context, "串碼已存在", Toast.LENGTH_SHORT).show();
                                    return;
                                }
                            }
                            stringCodeDetailEdit.setText("");
                            list.add(new GoodBean.DataBean.GoodsBean.SerialBean(
                                    bean.getMain_serial_number(), bean.getAuxiliary_serial_number_one(), bean.getAuxiliary_serial_number_two()));
                            adapter.setList(list);
                            setSize();
                        } else {
                            Toast.makeText(context, json.getString("message"), Toast.LENGTH_SHORT).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                protected void onFailure(ResponseException e) {
                    loadingDialog.dismiss();
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void setSize() {
        switch (operation) {
            case 1:
                stringCodeDetailChooseNum.setText(list.size() + "");
                break;
            case 2:
            case 3:
                stringCodeDetailChooseNum.setText(list1.size() + "");
                break;
            case 5:
            case 6:
                stringCodeDetailChooseNum.setText(list3.size() + "");
                break;
            case 7:
            case 8:
            case 9:
                stringCodeDetailChooseNum.setText(list4.size() + "");
                break;
            case 11:
                stringCodeDetailChooseNum.setText(list5.size() + "");
                break;
            case 12:
                stringCodeDetailChooseNum.setText(list6.size() + "");
                break;
            case 14:
            case 15:
                stringCodeDetailChooseNum.setText(list7.size() + "");
                break;
            case 16:
                int s = 0;
                for (WarehousingDetailInfo.DataBean.SerialListBean bean : list8) {
                    if (!bean.isCheck()) {
                        s++;
                    }
                }
                stringCodeDetailChooseNum.setText(s + "");
                break;
            case 27:
                stringCodeDetailChooseNum.setText(list9.size() + "/" + orderDetailInfo.getNum());
                break;
            case 28:
                stringCodeDetailChooseNum.setText(list10.size() + "");
                break;
        }
        adapter.setJudgeList(judgeList);
    }

    /**
     * 判斷重複
     */
    private boolean judgeRepeat() {
        if (stringCodeDetailMainEdit.getText().toString().equals(stringCodeDetailMainEdit1.getText().toString().trim())) {
            Toast.makeText(context, "串碼重複", Toast.LENGTH_SHORT).show();
            return true;
        }
        if (stringCodeDetailMainEdit.getText().toString().equals(stringCodeDetailMainEdit2.getText().toString().trim())) {
            Toast.makeText(context, "串碼重複", Toast.LENGTH_SHORT).show();
            return true;
        }
        if (!TextUtils.isEmpty(stringCodeDetailMainEdit1.getText().toString()) && stringCodeDetailMainEdit1.getText().toString().equals(stringCodeDetailMainEdit2.getText().toString().trim())) {
            Toast.makeText(context, "串碼重複", Toast.LENGTH_SHORT).show();
            return true;
        }
        if (judgeList.indexOf(stringCodeDetailMainEdit.getText().toString()) != -1) {
            Toast.makeText(context, "主串碼重複", Toast.LENGTH_SHORT).show();
            return true;
        }

        if (!TextUtils.isEmpty(stringCodeDetailMainEdit1.getText().toString()) && judgeList.indexOf(stringCodeDetailMainEdit1.getText().toString()) != -1) {
            Toast.makeText(context, "輔助串碼1重複", Toast.LENGTH_SHORT).show();
            return true;
        }
        if (!TextUtils.isEmpty(stringCodeDetailMainEdit2.getText().toString()) && judgeList.indexOf(stringCodeDetailMainEdit2.getText().toString()) != -1) {
            Toast.makeText(context, "輔助串碼2重複", Toast.LENGTH_SHORT).show();
            return true;
        }
        return false;
    }

    private void change() {
        stringCodeDetailNameLinear.setVisibility(View.GONE);
        stringCodeDetailSkuLinear.setVisibility(View.GONE);
        stringCodeDetailSearchLinear.setVisibility(View.GONE);
        stringCodeDetailAddLinear.setVisibility(View.GONE);
        switch (operation) {
            case 1:
                goodsBean = (GoodBean.DataBean.GoodsBean) getIntent().getSerializableExtra("good");
                stringCodeDetailNameLinear.setVisibility(View.VISIBLE);
                stringCodeDetailName.setText(goodsBean.getGoods_name());
                stringCodeDetailSearchLinear.setVisibility(View.VISIBLE);
                if (!TextUtils.isEmpty(goodsBean.getSku_name())) {
                    stringCodeDetailSkuLinear.setVisibility(View.VISIBLE);
                    stringCodeDetailSku.setText(goodsBean.getSku_name());
                }
                list.addAll(goodsBean.getSerialList() );
                for (GoodBean.DataBean.GoodsBean.SerialBean bean : list) {
                    judgeList.add(bean.getMain_serial_number());
                    if (!TextUtils.isEmpty(bean.getAuxiliary_serial_number_one())) {
                        judgeList.add(bean.getAuxiliary_serial_number_one());
                    }
                    if (!TextUtils.isEmpty(bean.getAuxiliary_serial_number_two())) {
                        judgeList.add(bean.getAuxiliary_serial_number_two());
                    }
                }
                setSize();
                adapter.setList(list);
                break;
            case 2:
            case 3:
                titleTitle.setText("添加串碼");
                reportLossBean = (ReportLossGoodBean.DataBean.ListBean) getIntent().getSerializableExtra("good");
                stringCodeDetailNameLinear.setVisibility(View.VISIBLE);
                stringCodeDetailName.setText(reportLossBean.getGoods_name());
                stringCodeDetailAddLinear.setVisibility(View.VISIBLE);
                if (!TextUtils.isEmpty(reportLossBean.getSku_name())) {
                    stringCodeDetailSkuLinear.setVisibility(View.VISIBLE);
                    stringCodeDetailSku.setText(reportLossBean.getSku_name());
                }
                list1 = reportLossBean.getSerialList();
                for (ReportLossGoodBean.DataBean.ListBean.SerialBean bean : list1) {
                    judgeList.add(bean.getMain_serial_number());
                    if (!TextUtils.isEmpty(bean.getAuxiliary_serial_number_one())) {
                        judgeList.add(bean.getAuxiliary_serial_number_one());
                    }
                    if (!TextUtils.isEmpty(bean.getAuxiliary_serial_number_two())) {
                        judgeList.add(bean.getAuxiliary_serial_number_two());
                    }
                }
                setSize();
                adapter.setList(list1);
                break;
            case 4:
            case 10:
            case 13:
            case 17:
            case 18:
            case 19:
            case 20:
            case 21:
            case 22:
            case 23:
            case 24:
            case 25:
            case 26:
                titleTitle.setText("串碼詳情");
//                stringCodeDetailList.setVisibility(View.GONE);
//                stringCodeDetailRefreshList.setVisibility(View.VISIBLE);
                stringCodeDetailSearchLinear.setVisibility(View.VISIBLE);
                stringCodeDetailSubmitLinear.setVisibility(View.GONE);
                stringCodeDetailAdd.setVisibility(View.GONE);
                id = getIntent().getStringExtra("id");
                getSerialDetail();
                break;
            case 29:
            case 30:
                reportLossBean = (ReportLossGoodBean.DataBean.ListBean) getIntent().getSerializableExtra("good");
                titleTitle.setText("串碼詳情");
//                stringCodeDetailList.setVisibility(View.GONE);
//                stringCodeDetailRefreshList.setVisibility(View.VISIBLE);
                stringCodeDetailSearchLinear.setVisibility(View.VISIBLE);
                stringCodeDetailSubmitLinear.setVisibility(View.GONE);
                stringCodeDetailAdd.setVisibility(View.GONE);
                id = getIntent().getStringExtra("id");
                getSerialDetail();
                break;
            case 5:
            case 6:
                titleTitle.setText("添加串碼");
                lossGoodsInfo = (LossGoodsInfo.DataBean) getIntent().getSerializableExtra("good");
                stringCodeDetailNameLinear.setVisibility(View.VISIBLE);
                stringCodeDetailSearchLinear.setVisibility(View.VISIBLE);
                stringCodeDetailName.setText(lossGoodsInfo.getGoods_name());
                if (!TextUtils.isEmpty(lossGoodsInfo.getSku_name())) {
                    stringCodeDetailSkuLinear.setVisibility(View.VISIBLE);
                    stringCodeDetailSku.setText(lossGoodsInfo.getSku_name());
                }
                list3 = lossGoodsInfo.getSerialList();
//                for (ReportLossGoodBean.DataBean.ListBean.SerialBean bean : list1) {
                for (LossGoodsInfo.DataBean.SerialBean bean : list3) {
                    judgeList.add(bean.getMain_serial_number());
                    if (!TextUtils.isEmpty(bean.getAuxiliary_serial_number_one())) {
                        judgeList.add(bean.getAuxiliary_serial_number_one());
                    }
                    if (!TextUtils.isEmpty(bean.getAuxiliary_serial_number_two())) {
                        judgeList.add(bean.getAuxiliary_serial_number_two());
                    }
                }
                setSize();
                adapter.setList(list3);
                break;
            case 7:
            case 8:
            case 9:
                titleTitle.setText("添加串碼");
                depositGoodsInfo = (DepositGoodsInfo.DataBean.ListBean) getIntent().getSerializableExtra("good");
                stringCodeDetailNameLinear.setVisibility(View.VISIBLE);
                stringCodeDetailName.setText(depositGoodsInfo.getGoods_name());
                stringCodeDetailAddLinear.setVisibility(View.VISIBLE);
                if (!TextUtils.isEmpty(depositGoodsInfo.getSku_name())) {
                    stringCodeDetailSkuLinear.setVisibility(View.VISIBLE);
                    stringCodeDetailSku.setText(depositGoodsInfo.getSku_name());
                }
                list4 = depositGoodsInfo.getSerialList();
                judgeList = getIntent().getStringArrayListExtra("list");
                setSize();
                adapter.setList(list4);
                break;
            case 11:
                titleTitle.setText("添加串碼");
                depositGoodsEditInfo = (DepositGoodsEditInfo.DataBean) getIntent().getSerializableExtra("good");
                stringCodeDetailNameLinear.setVisibility(View.VISIBLE);
                stringCodeDetailName.setText(depositGoodsEditInfo.getGoods_name());
                stringCodeDetailAddLinear.setVisibility(View.VISIBLE);
                if (!TextUtils.isEmpty(depositGoodsEditInfo.getSku_name())) {
                    stringCodeDetailSkuLinear.setVisibility(View.VISIBLE);
                    stringCodeDetailSku.setText(depositGoodsEditInfo.getSku_name());
                }
                list5 = depositGoodsEditInfo.getSerial_list();
                for (DepositGoodsEditInfo.DataBean.SerialListBean bean : list5) {
                    judgeList.add(bean.getMain_serial_number());
                    if (!TextUtils.isEmpty(bean.getAuxiliary_serial_number_one())) {
                        judgeList.add(bean.getAuxiliary_serial_number_one());
                    }
                    if (!TextUtils.isEmpty(bean.getAuxiliary_serial_number_two())) {
                        judgeList.add(bean.getAuxiliary_serial_number_two());
                    }
                }
                setSize();
                adapter.setList(list5);
                break;
            case 12:
                titleTitle.setText("選擇串碼");
                storeRetreatInfo = (GoodsStoreRetreatInfo.DataBean) getIntent().getSerializableExtra("good");
                stringCodeDetailNameLinear.setVisibility(View.VISIBLE);
                stringCodeDetailName.setText(storeRetreatInfo.getGoods_name());
                stringCodeDetailSearchLinear.setVisibility(View.VISIBLE);
                stringCodeDetailAdd.setVisibility(View.GONE);
                if (!TextUtils.isEmpty(storeRetreatInfo.getSku_name())) {
                    stringCodeDetailSkuLinear.setVisibility(View.VISIBLE);
                    stringCodeDetailSku.setText(storeRetreatInfo.getSku_name());
                }
                list6 = storeRetreatInfo.getSerial_list();
                for (GoodsStoreRetreatInfo.DataBean.SerialListBean bean : list6) {
                    judgeList.add(bean.getMain_serial_number());
                    if (!TextUtils.isEmpty(bean.getAuxiliary_serial_number_one())) {
                        judgeList.add(bean.getAuxiliary_serial_number_one());
                    }
                    if (!TextUtils.isEmpty(bean.getAuxiliary_serial_number_two())) {
                        judgeList.add(bean.getAuxiliary_serial_number_two());
                    }
                }
                setSize();
                adapter.setList(list6);
                break;
            case 14:
            case 15:
                titleTitle.setText("選擇串碼");
                allocationGoodsInfo = (AllocationGoodsInfo.DataBean) getIntent().getSerializableExtra("good");
                stringCodeDetailNameLinear.setVisibility(View.VISIBLE);
                stringCodeDetailName.setText(allocationGoodsInfo.getGoods_name());
                stringCodeDetailSearchLinear.setVisibility(View.VISIBLE);
                stringCodeDetailAdd.setVisibility(View.GONE);
                if (!TextUtils.isEmpty(allocationGoodsInfo.getSku_name())) {
                    stringCodeDetailSkuLinear.setVisibility(View.VISIBLE);
                    stringCodeDetailSku.setText(allocationGoodsInfo.getSku_name());
                }
                list7 = allocationGoodsInfo.getSerial_list();
                for (AllocationGoodsInfo.DataBean.SerialListBean bean : list7) {
                    judgeList.add(bean.getMain_serial_number());
                    if (!TextUtils.isEmpty(bean.getAuxiliary_serial_number_one())) {
                        judgeList.add(bean.getAuxiliary_serial_number_one());
                    }
                    if (!TextUtils.isEmpty(bean.getAuxiliary_serial_number_two())) {
                        judgeList.add(bean.getAuxiliary_serial_number_two());
                    }
                }
                setSize();
                adapter.setList(list7);
                break;
            case 16:
                titleTitle.setText("選擇串碼");
                warehousingDetailInfo = (WarehousingDetailInfo.DataBean) getIntent().getSerializableExtra("good");
                stringCodeDetailNameLinear.setVisibility(View.VISIBLE);
                stringCodeDetailName.setText(warehousingDetailInfo.getGoods_name());
                stringCodeDetailSearchLinear.setVisibility(View.VISIBLE);
                stringCodeDetailAdd.setVisibility(View.GONE);
                if (!TextUtils.isEmpty(warehousingDetailInfo.getSku_name())) {
                    stringCodeDetailSkuLinear.setVisibility(View.VISIBLE);
                    stringCodeDetailSku.setText(warehousingDetailInfo.getSku_name());
                }
                list8 = warehousingDetailInfo.getSerialList();
                for (WarehousingDetailInfo.DataBean.SerialListBean bean : list8) {
                    judgeList.add(bean.getMain_serial_number());
                    if (!TextUtils.isEmpty(bean.getAuxiliary_serial_number_one())) {
                        judgeList.add(bean.getAuxiliary_serial_number_one());
                    }
                    if (!TextUtils.isEmpty(bean.getAuxiliary_serial_number_two())) {
                        judgeList.add(bean.getAuxiliary_serial_number_two());
                    }
                }
                setSize();
                adapter.setList(list8);
                break;
            case 27:
                titleTitle.setText("選擇串碼");
                orderDetailInfo = (OrderDetailInfo.DataBean) getIntent().getSerializableExtra("good");
                stringCodeDetailNameLinear.setVisibility(View.VISIBLE);
                stringCodeDetailName.setText(orderDetailInfo.getGoods_name());
                stringCodeDetailSearchLinear.setVisibility(View.VISIBLE);
                stringCodeDetailAdd.setVisibility(View.GONE);
                if (!TextUtils.isEmpty(orderDetailInfo.getSku_name())) {
                    stringCodeDetailSkuLinear.setVisibility(View.VISIBLE);
                    stringCodeDetailSku.setText(orderDetailInfo.getSku_name());
                }
                list9 = orderDetailInfo.getSerial_list();
                for (OrderDetailInfo.DataBean.SerialListBean bean : list9) {
                    judgeList.add(bean.getMain_serial_number());
                    if (!TextUtils.isEmpty(bean.getAuxiliary_serial_number_one())) {
                        judgeList.add(bean.getAuxiliary_serial_number_one());
                    }
                    if (!TextUtils.isEmpty(bean.getAuxiliary_serial_number_two())) {
                        judgeList.add(bean.getAuxiliary_serial_number_two());
                    }
                }
                setSize();
                adapter.setList(list9);
                break;
            case 28:
                titleTitle.setText("選擇串碼");
                onlineOrderBean = (OnlineOrderBean.OrderBean.GoodInfoBean) getIntent().getSerializableExtra("good");
                stringCodeDetailNameLinear.setVisibility(View.VISIBLE);
                stringCodeDetailName.setText(onlineOrderBean.getName());
                stringCodeDetailAdd.setVisibility(View.GONE);
                list10 = onlineOrderBean.getSerial_list();
                for (OnlineOrderBean.OrderBean.GoodInfoBean.SerialListBean bean : list10) {
                    judgeList.add(bean.getMain_serial_number());
                    if (!TextUtils.isEmpty(bean.getAuxiliary_serial_number_one())) {
                        judgeList.add(bean.getAuxiliary_serial_number_one());
                    }
                    if (!TextUtils.isEmpty(bean.getAuxiliary_serial_number_two())) {
                        judgeList.add(bean.getAuxiliary_serial_number_two());
                    }
                }
                setSize();
                adapter.setList(list10);
                break;

        }
    }

    /**
     * 獲取商品串碼列表
     */
    private void getSerialDetail() {
        loadingDialog.show();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("id", id);
            jsonObject.put("user_type", aCache.getAsString("userType"));
            jsonObject.put("serial_number", stringCodeDetailEdit.getText().toString().trim());
            jsonObject.put("page", page);
            jsonObject.put("limit", 20);
            String url = "";
            switch (operation) {
                case 4:
                    url = Common.INVENTORY_SERIAL;
                    break;
                case 10://採購商品串碼詳情
                    if ("1".equals(aCache.getAsString("userType"))) {
                        url = Common.MER_PUR_DETAIL_SERIAL_LIST;
                    } else if ("2".equals(aCache.getAsString("userType"))) {
                        url = Common.STORE_PUR_DETAIL_SERIAL_LIST;
                    }
                    break;
                case 13://採購退貨商品串碼詳情
                    if ("1".equals(aCache.getAsString("userType"))) {
                        url = Common.MER_RETREAT_DETAIL_SERIAL;
                    } else if ("2".equals(aCache.getAsString("userType"))) {
                        url = Common.STORE_RETREAT_DETAIL_SERIAL;
                    }
                    break;
                case 17://出庫詳情 商戶門店
                    url = Common.STOCK_GOODS_INFO_SERIAL;
                    break;
                case 18://入庫詳情 商戶出庫單
                    url = Common.STORE_DETAIL_SERIAL;
                    break;
                case 19://拒收詳情 商戶門店
                    url = Common.REFUSE_SERIAL;
                    break;
                case 20://調撥詳情 商戶
                    jsonObject.put("user_type", "2");
                    url = Common.STOCK_GOODS_INFO_SERIAL;
                    break;
                case 21://商戶調撥單 入庫詳情
                    jsonObject.put("user_type", "2");
                    url = Common.STORE_DETAIL_SERIAL;
                    break;
                case 22://門店調撥單(商戶調撥)
                    jsonObject.put("user_type", "1");
                    url = Common.STOCK_GOODS_INFO_SERIAL;
                    break;
                case 23:
                    jsonObject.put("user_type", aCache.getAsString("userType"));
                    url = Common.STOCK_GOODS_INFO_SERIAL;
                    break;
                case 24://門店入庫單(入庫詳情)
                    jsonObject.put("user_type", aCache.getAsString("userType"));
                    url = Common.STORE_DETAIL_SERIAL;
                    break;
                case 25:
                    if ("1".equals(aCache.getAsString("userType"))) {
                        url = Common.LOSS_INFO_SERIAL;
                    } else if ("2".equals(aCache.getAsString("userType"))) {
                        url = Common.STORE_LOSS_INFO_SERIAL;
                    }
                    break;
                case 26:
//                    url = Common.STORE_LOSS_INFO_SERIAL;
                    url = Common.SELL_SERIAL_LIST;
                    break;
                case 29:
                case 30:
                    jsonObject.put("goods_id", reportLossBean.getGoods_id());
                    jsonObject.put("sku_id", reportLossBean.getSku_id());
                    url = Common.SALE_SERIAL_LIST;
                    break;
            }
            HttpUtils.post(context, url, Tools.createParameterCode(jsonObject), new TextCallBack() {
                @Override
                protected void onSuccess(String text) {
                    loadingDialog.dismiss();
                    stringCodeDetailRefreshList.onRefreshComplete();
                    try {
                        JSONObject json = new JSONObject(text);
                        if (json.getInt("status") == 1) {
                            page++;
                            serialDetailInfo = GsonUtils.JsonToBean(text, SerialDetailInfo.class);
                            list2.addAll(serialDetailInfo.getData().getList());
                            adapter.setList(list2);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                protected void onFailure(ResponseException e) {
                    loadingDialog.dismiss();
                    stringCodeDetailRefreshList.onRefreshComplete();
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /**
     * 5,6報損選擇串碼
     */
    private void chooseGoodSerial() {
        loadingDialog.show();
        JSONObject jsonObject = new JSONObject();
        try {
            if (operation==1){
                jsonObject.put("goods_id", goodsBean.getId());
                jsonObject.put("sku_id", goodsBean.getSku_id());
                jsonObject.put("serial_number", stringCodeDetailEdit.getText().toString().trim());
//            jsonObject.put("exists_id_arr", "");
                jsonObject.put("page", page);
                jsonObject.put("limit", 20);
            }else {
                jsonObject.put("goods_id", lossGoodsInfo.getGoods_id());
                jsonObject.put("sku_id", lossGoodsInfo.getSku_id());
                jsonObject.put("serial_number", stringCodeDetailEdit.getText().toString().trim());
//            jsonObject.put("exists_id_arr", "");
                jsonObject.put("page", page);
                jsonObject.put("limit", 20);
            }

            String url = "";
            url = Common.LOSS_GOODS_SERIAL;
            HttpUtils.post(context, url, Tools.createParameterCode(jsonObject), new TextCallBack() {
                @Override
                protected void onSuccess(String text) {
                    loadingDialog.dismiss();
                    try {
                        JSONObject json = new JSONObject(text);
                        if (json.getInt("status") == 1) {
                            retreatGoodSerialList = GsonUtils.JsonToBean(text, RetreatGoodSerialList.class);
                            if (retreatGoodSerialList.getData().getList().size() == 0) {
                                Toast.makeText(context, "找不到當前串碼", Toast.LENGTH_SHORT).show();
                                return;
                            }
                            for (RetreatGoodSerialList.DataBean.ListBean b : retreatGoodSerialList.getData().getList()) {
                                list3.add(new LossGoodsInfo.DataBean.SerialBean(b.getId(), b.getMain_serial_number(),
                                        b.getAuxiliary_serial_number_one(), b.getAuxiliary_serial_number_two()));
                                stringCodeDetailMainEdit.setText("");
                                stringCodeDetailMainEdit1.setText("");
                                stringCodeDetailMainEdit2.setText("");
                                adapter.setList(list3);
                                Log.d("測試9",b.getMain_serial_number()+"："+b.getAuxiliary_serial_number_one()+"："+b.getAuxiliary_serial_number_two());
                                judgeList.add(b.getMain_serial_number());
                                if (!TextUtils.isEmpty(b.getAuxiliary_serial_number_one())) {
                                    judgeList.add(b.getAuxiliary_serial_number_one());
                                }
                                if (!TextUtils.isEmpty(b.getAuxiliary_serial_number_two())) {
                                    judgeList.add(b.getAuxiliary_serial_number_two());
                                }
                            }
                            setSize();
                            stringCodeDetailEdit.setText("");
                        } else {
                            Toast.makeText(context, json.getString("message"), Toast.LENGTH_SHORT).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                protected void onFailure(ResponseException e) {
                    loadingDialog.dismiss();
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /**
     * 盤點判斷驗證串碼列表
     */
    private void inventorySerialVerify() {
        loadingDialog.show();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("goods_id", reportLossBean.getGoods_id());
            jsonObject.put("sku_id", reportLossBean.getSku_id());
            JSONArray array = new JSONArray();
            for (ReportLossGoodBean.DataBean.ListBean.SerialBean bean : list1) {
                JSONObject j = new JSONObject();
                j.put("main_serial_number", bean.getMain_serial_number());
                j.put("auxiliary_serial_number_one", bean.getAuxiliary_serial_number_one());
                j.put("auxiliary_serial_number_two", bean.getAuxiliary_serial_number_two());
                array.put(j);
            }
            jsonObject.put("serial_list", array);
            HttpUtils.post(context, Common.INVENTORY_SERIAL_VERIFY, Tools.createParameterCode(jsonObject), new TextCallBack() {
                @Override
                protected void onSuccess(String text) {
                    loadingDialog.dismiss();
                    try {
                        JSONObject json = new JSONObject(text);
                        Toast.makeText(context, json.getString("message"), Toast.LENGTH_SHORT).show();
                        if (json.getInt("status") == 1) {
                            reportLossBean.setSerialList((List<ReportLossGoodBean.DataBean.ListBean.SerialBean>) list1);
                            reportLossBean.setInventory_num(list1.size() + "");
                            if (operation == 2) {
                                intent = new Intent(context, NewInventoryActivity.class);
                            } else {
                                intent = new Intent(context, InventorySearchActivity.class);
                            }
                            intent.putExtra("bean", reportLossBean);
                            setResult(RESULT_OK, intent);
                            finish();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }

                @Override
                protected void onFailure(ResponseException e) {
                    loadingDialog.dismiss();
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    /**
     * 採購判斷驗證串碼列表
     */
    private void depositSerialVerify() {
        loadingDialog.show();
        JSONObject jsonObject = new JSONObject();
        try {
            if (operation == 7 || operation == 8 || operation == 9) {
                jsonObject.put("goods_id", depositGoodsInfo.getGoods_id());
                jsonObject.put("sku_id", depositGoodsInfo.getSku_id());
            } else {
                jsonObject.put("goods_id", depositGoodsEditInfo.getGoods_id());
                jsonObject.put("sku_id", depositGoodsEditInfo.getSku_id());
            }
            String url = "";
            if ("1".equals(aCache.getAsString("userType"))) {
                url = Common.MER_PUR_GOODS_SERIAL_VERIFY;
            } else if ("2".equals(aCache.getAsString("userType"))) {
                url = Common.STORE_PUR_GOODS_SERIAL_VERIFY;
            }
            JSONArray array = new JSONArray();
            if (operation == 7 || operation == 8 || operation == 9) {
                for (DepositGoodsInfo.DataBean.ListBean.SerialBean bean : list4) {
                    JSONObject j = new JSONObject();
                    j.put("main_serial_number", bean.getMain_serial_number());
                    j.put("auxiliary_serial_number_one", bean.getAuxiliary_serial_number_one());
                    j.put("auxiliary_serial_number_two", bean.getAuxiliary_serial_number_two());
                    array.put(j);
                }
            } else if (operation == 11) {
                for (DepositGoodsEditInfo.DataBean.SerialListBean bean : list5) {
                    JSONObject j = new JSONObject();
                    j.put("id", bean.getId());
                    j.put("main_serial_number", bean.getMain_serial_number());
                    j.put("auxiliary_serial_number_one", bean.getAuxiliary_serial_number_one());
                    j.put("auxiliary_serial_number_two", bean.getAuxiliary_serial_number_two());
                    array.put(j);
                }
            }
            jsonObject.put("serial_list", array);
            HttpUtils.post(context, url, Tools.createParameterCode(jsonObject), new TextCallBack() {
                @Override
                protected void onSuccess(String text) {
                    loadingDialog.dismiss();
                    try {
                        JSONObject json = new JSONObject(text);
                        Toast.makeText(context, json.getString("message"), Toast.LENGTH_SHORT).show();
                        if (json.getInt("status") == 1) {
                            if (operation == 7 || operation == 11) {
                                intent = new Intent(context, PurchaseOrderAddActivity.class);
                            } else {
                                intent = new Intent(context, DepositGoodsActivity.class);
                            }
                            if (operation == 8) {
                                depositGoodsInfo.setOutput_num(list4.size() + "");
                                depositGoodsInfo.setSerialList(list4);
                                intent.putExtra("bean", depositGoodsInfo);
                            } else if (operation == 7 || operation == 9) {
                                depositGoodsInfo.setPurchase_num(list4.size() + "");
                                depositGoodsInfo.setSerialList(list4);
                                intent.putExtra("bean", depositGoodsInfo);
                            } else if (operation == 11) {
                                depositGoodsEditInfo.setPurchase_num((double) list5.size());
                                depositGoodsEditInfo.setSerial_list(list5);
                                intent.putExtra("bean", depositGoodsEditInfo);
                            }
                            setResult(RESULT_OK, intent);
                            finish();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }

                @Override
                protected void onFailure(ResponseException e) {
                    loadingDialog.dismiss();
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    /**
     * 12 採購退貨獲取可退串碼列表 14 出庫獲取串碼列表
     */
    private void retreatGoodSerial() {
        loadingDialog.show();
        JSONObject jsonObject = new JSONObject();
        try {
            if (operation == 12) {
                jsonObject.put("id", storeRetreatInfo.getId());
            } else if (operation == 14 || operation == 15) {
                jsonObject.put("goods_id", allocationGoodsInfo.getGoods_id());
                jsonObject.put("sku_id", allocationGoodsInfo.getSku_id());
            }
            jsonObject.put("serial_number", stringCodeDetailEdit.getText().toString().trim());
//            jsonObject.put("exists_id_arr", "");
            jsonObject.put("page", page);
            jsonObject.put("limit", 20);
            String url = "";
            if (operation == 12) {
                if ("1".equals(aCache.getAsString("userType"))) {
                    url = Common.MER_RETREAT_GOODS_SERIAL;
                } else if ("2".equals(aCache.getAsString("userType"))) {
                    url = Common.STORE_RETREAT_GOODS_SERIAL;
                }
            } else if (operation == 14 || operation == 15) {
                url = Common.LOSS_GOODS_SERIAL;
            }
            HttpUtils.post(context, url, Tools.createParameterCode(jsonObject), new TextCallBack() {
                @Override
                protected void onSuccess(String text) {
                    loadingDialog.dismiss();
                    try {
                        JSONObject json = new JSONObject(text);
                        if (json.getInt("status") == 1) {
                            retreatGoodSerialList = GsonUtils.JsonToBean(text, RetreatGoodSerialList.class);
                            if (retreatGoodSerialList.getData().getList().size() == 0) {
                                Toast.makeText(context, "找不到當前串碼", Toast.LENGTH_SHORT).show();
                                return;
                            }
                            for (RetreatGoodSerialList.DataBean.ListBean b : retreatGoodSerialList.getData().getList()) {
                                if (operation == 12) {
                                    list6.add(new GoodsStoreRetreatInfo.DataBean.SerialListBean(
                                            b.getId(), b.getMain_serial_number(), b.getAuxiliary_serial_number_one(), b.getAuxiliary_serial_number_two()
                                    ));
                                } else if (operation == 14 || operation == 15) {
                                    list7.add(new AllocationGoodsInfo.DataBean.SerialListBean(
                                            b.getId(), b.getMain_serial_number(), b.getAuxiliary_serial_number_one(), b.getAuxiliary_serial_number_two()
                                    ));
                                }
                                judgeList.add(b.getMain_serial_number());
                                if (!TextUtils.isEmpty(b.getAuxiliary_serial_number_one())) {
                                    judgeList.add(b.getAuxiliary_serial_number_one());
                                }
                                if (!TextUtils.isEmpty(b.getAuxiliary_serial_number_two())) {
                                    judgeList.add(b.getAuxiliary_serial_number_two());
                                }
                            }
                            setSize();
                            if (operation == 12) {
                                adapter.setList(list6);
                            } else if (operation == 14 || operation == 15) {
                                adapter.setList(list7);
                            }
                            stringCodeDetailEdit.setText("");
                        } else {
                            Toast.makeText(context, json.getString("message"), Toast.LENGTH_SHORT).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                protected void onFailure(ResponseException e) {
                    loadingDialog.dismiss();
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /**
     * 27 配送商品搜索串碼
     */
    private void getDeliverySerial() {
        loadingDialog.show();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("goods_id", orderDetailInfo.getGoods_id());
            jsonObject.put("sku_id", orderDetailInfo.getSku_id());
            jsonObject.put("serial_number", stringCodeDetailEdit.getText().toString().trim());
            HttpUtils.post(context, Common.SEARCH_SERIAL, Tools.createParameterCode(jsonObject), new TextCallBack() {
                @Override
                protected void onSuccess(String text) {
                    loadingDialog.dismiss();
                    try {
                        JSONObject json = new JSONObject(text);
                        if (json.getInt("status") == 1) {
                            JSONObject data = json.getJSONObject("data");
                            list9.add(new OrderDetailInfo.DataBean.SerialListBean(
                                    data.getString("main_serial_number"),
                                    data.getString("auxiliary_serial_number_one"),
                                    data.getString("auxiliary_serial_number_two")
                            ));
                            judgeList.add(data.getString("main_serial_number"));
                            if (!TextUtils.isEmpty(data.getString("auxiliary_serial_number_one"))) {
                                judgeList.add(data.getString("auxiliary_serial_number_one"));
                            }
                            if (!TextUtils.isEmpty(data.getString("auxiliary_serial_number_two"))) {
                                judgeList.add(data.getString("auxiliary_serial_number_two"));
                            }
                            setSize();
                            adapter.setList(list9);
                            stringCodeDetailEdit.setText("");
                        } else {
                            Toast.makeText(context, json.getString("message"), Toast.LENGTH_SHORT).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                protected void onFailure(ResponseException e) {
                    loadingDialog.dismiss();
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            Bundle bundle = data.getExtras();
            switch (requestCode) {
                case Constant.REQ_QR_CODE:
                    String scan_msg = bundle.getString(Constant.INTENT_EXTRA_KEY_QR_SCAN);
                    switch (scanType) {
                        case 0:
                            stringCodeDetailEdit.setText(scan_msg);
                            if (operation == 27) {
                                if (TextUtils.isEmpty(stringCodeDetailEdit.getText().toString().trim())) {
                                    Toast.makeText(context, "請輸入要搜索的串碼", Toast.LENGTH_SHORT).show();
                                    return;
                                }
                                if (judgeList.indexOf(stringCodeDetailEdit.getText().toString().trim()) != -1) {
                                    Toast.makeText(context, "串碼重複", Toast.LENGTH_SHORT).show();
                                    return;
                                }
                                if (list9.size() >= orderDetailInfo.getNum()) {
                                    Toast.makeText(context, "不能繼續添加了", Toast.LENGTH_SHORT).show();
                                    return;
                                }
                                getDeliverySerial();
                            }
                            break;
                        case 1:
                            stringCodeDetailMainEdit.setText(scan_msg);
                            break;
                        case 2:
                            stringCodeDetailMainEdit1.setText(scan_msg);
                            break;
                        case 3:
                            stringCodeDetailMainEdit2.setText(scan_msg);
                            break;
                    }
                    break;
            }
        }
    }


}
